package com.alibaba.dingtalk.openapi.helper;


import com.alibaba.dingtalk.openapi.message.ConversationMessageDelivery;
import com.alibaba.dingtalk.openapi.message.LightAppMessageDelivery;
import com.alibaba.dingtalk.openapi.exception.OApiException;
import com.alibaba.dingtalk.openapi.exception.OApiResultException;
import com.alibaba.dingtalk.openapi.utils.HttpHelper;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author kaka
 */
public class MessageHelper {
	private static final Logger LOG = LoggerFactory.getLogger(MessageHelper.class);
	/**
	 * 如果发送消息太多，则延迟3600s后再发送
	 */
	public static int SEND_DELAY_TIME = 3600;
	public static class Receipt {
		String invaliduser;
		String invalidparty;
	}
	
	
	public static Receipt send(String accessToken, LightAppMessageDelivery delivery)
			throws OApiException {
		String url = Env.OAPI_HOST + "/message/send?" +
				"access_token=" + accessToken;

		JSONObject response = new JSONObject();
		try {
			response = HttpHelper.httpPost(url, delivery.toJsonObject());
		} catch (Exception e) {
			LOG.error("访问/message/send出错", e);
			throw e;
		}
		
		//errcode 90008 
		if(response.containsKey("errcode") ){
			int errcode = response.getInteger("errcode") ;
			String msg = response.getString("msg");
			if(90008 == errcode){
			    // 请求总次数超过100，当天不可再发送
				LOG.error("send too many messages:"+delivery.getCorpid()+msg);
 				throw new OApiResultException("send too many messages!");
				 
			}
		}
				
		if (response.containsKey("invaliduser") || response.containsKey("invalidparty")) {
			Receipt receipt = new Receipt();
			receipt.invaliduser = response.getString("invaliduser");
			receipt.invalidparty = response.getString("invalidparty");
			return receipt;
		}
		else {
			throw new OApiResultException("invaliduser or invalidparty");
		}
	}
	
	
	public static String send(String accessToken, ConversationMessageDelivery delivery)
		throws OApiException {
		String url = Env.OAPI_HOST + "/message/send_to_conversation?" +
				"access_token=" + accessToken;
		
		JSONObject response = HttpHelper.httpPost(url, delivery.toJsonObject());
		if (response.containsKey("receiver")) {
			return response.getString("receiver");
		}
		else {
			throw new OApiResultException("receiver");
		}
	}
	
	/**
	 * 反馈式消息推送
	 * https://oapi.dingtalk.com/message/sendByCode?access_token=ACCESS_TOKEN
	 * @param accessToken
	 * @param code
	 * @param delivery
	 * @return
	 * kaka
	 * 2016年12月29日 下午3:38:00
	 */
	public static Receipt sendByCode(String accessToken, String code, LightAppMessageDelivery delivery)
			throws OApiException {
		String url = Env.OAPI_HOST + "/message/sendByCode?" +
				"access_token=" + accessToken;
		
		JSONObject response = new JSONObject();
		try {
			delivery.code = code;
			response = HttpHelper.httpPost(url, delivery.toJsonObject());
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);

		}
		if (response.containsKey("invaliduser") || response.containsKey("invalidparty")) {
			Receipt receipt = new Receipt();
			receipt.invaliduser = response.getString("invaliduser");
			receipt.invalidparty = response.getString("invalidparty");
			return receipt;
		}
		else {
			throw new OApiResultException("invaliduser or invalidparty");
		}
	}
}
